package com.pet.system.security.shiro;

import com.pet.common.model.ResponseResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Shiro相关异常全局捕捉
 *
 * @author Centaurea
 */
@ControllerAdvice
public class ShiroExceptionHandler {

  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  @ResponseBody
  @ExceptionHandler(value = {AuthorizationException.class})
  public ResponseResult authorizationException(AuthorizationException e) {
    e.printStackTrace();
    return ResponseResult.error("无权访问");
  }

  @ResponseBody
  @ExceptionHandler(value = {SecurityException.class})
  public ResponseResult securityException(SecurityException e) {
    e.printStackTrace();
    return ResponseResult.error(e.getMessage());
  }

  @ResponseBody
  @ExceptionHandler(value = {AuthenticationException.class})
  public ResponseResult authenticationException(AuthenticationException e) {
    e.printStackTrace();
    return ResponseResult.error(e.getMessage());
  }

  @ResponseBody
  @ExceptionHandler(value = {Exception.class})
  public ResponseResult exception(Exception e) {
    e.printStackTrace();
    return ResponseResult.error(e.getMessage());
  }
}
